package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.AgentInfo;
import com.school.movie.business.web.model.vo.CityVo;
import com.school.movie.business.web.model.vo.Manager;
import com.school.movie.business.web.model.vo.ProvinceVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.utils.ContextUtils;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.AgentInfoServiceImpI;
import com.school.movie.service.imp.SysLogServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    AgentInfoServiceImpI agentService;
    @Autowired
    SysLogServiceImp logService;
    @Value("${upload.absPath}")
    String absPath;

    @RequestMapping("/list")
    public String list(AgentInfo agent, PageBean pager, HttpSession session, ModelMap map) {

        if (agent == null || agent.getAgentName() == null) {
            if (session.getAttribute("agent") != null) {
                agent = (AgentInfo) session.getAttribute("agent");
            }
        } else {
            session.setAttribute("agent", agent);
        }
        getParams(agent, pager, map);
        return "agent";
    }

    @RequestMapping("/cityList")
    @ResponseBody
    public Map cityList(int provinceId) {
        Map map = new HashMap();
        List<CityVo> cityList = agentService.city(provinceId);
        map.put("cityList", cityList);
        return map;
    }

    @RequestMapping("/myInfo")
    public String myInfo(ModelMap map) {
        Manager mgr = ContextUtils.getManagerInfo();
        if (mgr == null) {
            return "login";
        }
        AgentInfo agent = agentService.myInfo(mgr.getAgentId());
        map.addAttribute("agent", agent);
        return "agentMyInfo";
    }


    /**
     * 新增保存，修改保存
     *
     * @param request
     * @param session
     * @param file1
     * @param agent
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public String save(HttpServletRequest request, HttpSession session, MultipartFile file1, AgentInfo agent, ModelMap map) throws Exception {
        if (agent.getAgentId() > 0) {//修改
            AgentInfo g = agentService.getAgentById(agent.getAgentId());
            if (file1.getOriginalFilename().equals("")) {
                agent.setAgentIcon(g.getAgentIcon());
            } else {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                agent.setAgentIcon("card/" + newFileName);
            }
            agentService.update(agent);
            logService.add(BaseConstants.LOG_UPDATE, agent.toString());
        } else {
            //新增
            if (file1.getOriginalFilename().equals("")) {
                agent.setAgentIcon("");
            } else {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                agent.setAgentIcon("card/" + newFileName);
            }
            agentService.add(agent);
            //写日志
            logService.add(BaseConstants.LOG_ADD, agent.toString());
        }
        agent = new AgentInfo();
        if (session.getAttribute("agent") != null) {
            agent = (AgentInfo) session.getAttribute("agent");
        }
        map = getParams(agent, new PageBean(), map);
        return "agent";
    }

    private ModelMap getParams(AgentInfo agent, PageBean pager, ModelMap map) {
        int cnt = agentService.count(agent);
        pager.setTotalRows(cnt);
        List<AgentInfo> agentList = agentService.list(agent, pager);
        pager.setData(agentList);
        List<ProvinceVo> provinceList = agentService.province();
        List<CityVo> cityList = null;
        if (agent.getAgentId() > 0) {
            cityList = agentService.city(agent.getProvinceId());
        } else {
            cityList = agentService.city(provinceList.get(0).getProvinceId());
        }

        map.addAttribute("pager", pager);
        map.addAttribute("agent", agent);
        map.addAttribute("provinceList", provinceList);
        map.addAttribute("cityList", cityList);

        return map;
    }

    /**
     * 跳转影院添加页面
     *
     * @param map
     * @return
     */
    @RequestMapping("/add")
    public String add(ModelMap map) {
        List<ProvinceVo> provinceList = agentService.province();
        List<CityVo> cityList = agentService.city(provinceList.get(0).getProvinceId());
        AgentInfo agent = new AgentInfo();
        agent.setInvalid(1);
        map.addAttribute("provinceList", provinceList);
        map.addAttribute("cityList", cityList);
        map.addAttribute("agent", agent);
        return "agentAdd";
    }

    @RequestMapping("/update")
    public String update(AgentInfo agent, ModelMap map) {
        agent = agentService.getAgentById(agent.getAgentId());
        List<ProvinceVo> provinceList = agentService.province();
        List<CityVo> cityList = null;
        if (agent.getAgentId() > 0) {
            cityList = agentService.city(agent.getProvinceId());
        } else {
            cityList = agentService.city(provinceList.get(0).getProvinceId());
        }
        map.addAttribute("agent", agent);
        map.addAttribute("provinceList", provinceList);
        map.addAttribute("cityList", cityList);

        return "agentAdd";
    }

    @RequestMapping("/del")
    public String del(String ids, ModelMap map) {
        String[] arr = ids.split(",");
        agentService.del(arr);
        getParams(new AgentInfo(), new PageBean(), map);
        return "agent";
    }
}
